/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */

package com.pig4cloud.pigx.contract.mapper;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.pigx.common.customize.dto.TimeRangeDTO;
import com.pig4cloud.pigx.common.data.datascope.PigxBaseMapper;
import com.pig4cloud.pigx.contract.bo.ContractTradeNewBO;
import com.pig4cloud.pigx.contract.entity.ContractKLine;
import com.pig4cloud.pigx.contract.entity.ContractTrade;
import com.pig4cloud.pigx.contract.mongo.entity.ContractKLineTradeCacheMongo;
import com.pig4cloud.pigx.contract.thread.CalculateKLineThread;
import com.pig4cloud.pigx.contract.vo.ContractHistoryTradeVO;
import com.pig4cloud.pigx.contract.vo.ContractKLineVO;
import com.pig4cloud.pigx.contract.vo.ContractStatisticsHandlingFeeVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 成交记录表
 *
 * @date 2022-08-11 10:08:52
 */
@Mapper
@Component
public interface ContractTradeMapper extends PigxBaseMapper<ContractTrade> {

	/**
	 * 获取最新成交记录
	 *
	 * @param symbolId 币对id
	 * @return
	 */
	List<ContractKLineTradeCacheMongo> selectNewestTrade(@Param("symbolId") Long symbolId);

	/**
	 * {@link CalculateKLineThread} 查询k线数据
	 *
	 * @param timeRange 时间范围
	 * @return {@link List< ContractKLine>}
	 * @date 2022/8/15 21:30
	 */
	List<ContractKLineVO> selectForCalculateKLineThread(TimeRangeDTO timeRange);

	/**
	 * 查询K线开盘价
	 *
	 * @param dto 时间区间
	 * @return 开盘价
	 */
	BigDecimal selectOpenPrice(TimeRangeDTO dto);

	/**
	 * 查询K线收盘价
	 *
	 * @param dto 时间区间
	 * @return 收盘价
	 */
	BigDecimal selectClosePrice(TimeRangeDTO dto);

	/**
	 * 新增成交记录
	 *
	 * @param bo bo
	 * @return {@link int}
	 * @date 2022/8/20 17:14
	 */
	int insert(ContractTradeNewBO bo);

	/**
	 * app-获取历史成交记录
	 *
	 * @param page 分页
	 * @return
	 */
	IPage<ContractHistoryTradeVO> getHistoryTrade(Page page,
												  @Param("userId") Long userId,
												  @Param("startTime") LocalDateTime startTime,
												  @Param("endTime") LocalDateTime endTime,
												  @Param("symbolId") Long symbolId
	);

	/**
	 * 根据订单id查询平均成交价格
	 *
	 * @param orderId       订单id
	 * @param operationType 订单操作类型
	 * @return {@link BigDecimal}
	 * @since 2022/10/14 15:13
	 */
	BigDecimal selectAvgPriceByOrderId(@Param("orderId") Long orderId,
									   @Param("operationType") String operationType);

	/**
	 * 根据仓位id查询平均成交价格
	 *
	 * @param positionId    仓位id
	 * @param operationType 订单操作类型
	 * @return {@link BigDecimal}
	 * @since 2022/10/14 15:13
	 */
	BigDecimal selectAvgPriceByPositionId(@Param("positionId") Long positionId,
										  @Param("operationType") String operationType);

	/**
	 *  财务 -> 手续费统计报表
	 * @param startTime 开始时间
	 * @param endTime 结束时间
	 * @return list
	 */
	List<ContractStatisticsHandlingFeeVO> statisticsHandlingFee(@Param("startTime") LocalDateTime startTime,
																@Param("endTime") LocalDateTime endTime);
}
